package com.birdcage.goldminer.gameobjects;

import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector2;
import com.birdcage.gamemanagers.GameObject;
import com.birdcage.gamemanagers.GraphicsElement;
import com.birdcage.gamemanagers.GraphicsManager;
import com.birdcage.gamemanagers.PhysicsElement;
import com.birdcage.gamemanagers.GameObject.EGameObjectType;

public class GoldMinerObject extends GameObject {

    protected int score = 10;
    protected int weight = 10;
    
    protected boolean isCaught = false;
    protected boolean isBlasted = false;
    
    protected Vector2 deltaVec = new Vector2();
    protected Vector2 originVec = new Vector2();
    
    
    public GoldMinerObject() {
        super(EGameObjectType.eGOT_Monster, 0);
        
        InitFSMAndCollide();
    }
    
    public GoldMinerObject(int score, int weight) {
        super(EGameObjectType.eGOT_Monster, 0);
        
        InitFSMAndCollide();
        
        this.score = score;
        this.weight = weight;
    }
    
    private void InitFSMAndCollide() {
        try
        {            
            GetFSM().AddState("Idle",this);
            GetFSM().AddState("Return", this);
            GetFSM().AddState("Blast", this);
            GetFSM().AddState("Reclaim", this);
            GetFSM().InitState("Idle");
        } catch (SecurityException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        SetPhysicsCallback(this, "Collide");

    }
    
    //FSM callbacks
    public void Idle_Enter() {
        Gdx.app.log("GoldMinerObject", "Idle_Enter");
    }
    
    public void Idle_Update() {
        GetFSM().SwitchState(isCaught, "Return");

        GetFSM().SwitchState(isBlasted, "Blast");
    }

    public void Idle_Exit() {
        isBlasted = false;
        isCaught = false;
    }
    
    public void Blast_Enter() {
        m_physElem.SetActive(false);

        ArrayList<GraphicsElement> renderUnit = GetRenderUnit();
        for (int i = 0, size = renderUnit.size(); i < size; ++i) {
            GraphicsManager.GetInstance().RemoveElement(renderUnit.get(i));
        }
    }
    
    public void Blast_Update() {
        Gdx.app.log("GoldMinerObject", "Blast_Update");
    }
    
    public void Blast_Exit() {
        Gdx.app.log("GoldMinerObject", "Blast_Exit");
    }
    
    public void Return_Enter() {
        Gdx.app.log("GoldMinerObject", "Return_Enter");
    }
    
    public void Return_Update() {
        Gdx.app.log("Stone", "Return_Update");

        deltaVec.set(GetWorldPos());
        GetFSM().SwitchState(deltaVec.dst(originVec) <= 10, "Reclaim");
    }
    
    public void Return_Exit() {
        Gdx.app.log("GoldMinerObject", "Return_Exit");
    }
    
    public void Reclaim_Enter() {
        //remove self from PM
        m_physElem.SetActive(false);
        //remove self from GM
        ArrayList<GraphicsElement> renderUnit = GetRenderUnit();
        for (int i = 0, size = renderUnit.size(); i < size; ++i) {
            GraphicsManager.GetInstance().RemoveElement(renderUnit.get(i));
        }
    }
    
    public void Reclaim_Update() {
        Gdx.app.log("GoldMinerObject", "Reclaim_Update");
    }
    
    public void Reclaim_Exit() {
        Gdx.app.log("GoldMinerObject", "Reclaim_Exit");
    }
    
    //collide 
    public void Collide_Enter(PhysicsElement phyElem) {
        Gdx.app.log("GoldMinerObject", "Collide_Enter");
        
        if (phyElem.GetParent().GetType() == EGameObjectType.eGOT_Player) {
            //set hook's velocity to stone
            m_physElem.SetVelocity(phyElem.GetVelocity());
            m_physElem.SetDir(phyElem.GetDir());
            originVec.set(((Hook)(phyElem.GetParent())).GetRotateOrigin());
            isCaught = true;
        }
        if (phyElem.GetParent().GetType() == EGameObjectType.eGOT_Monster) {
            isBlasted = true;
        }
    }
    
    public void Collide_Update(PhysicsElement phyElem) {
        Gdx.app.log("GoldMinerObject", "Collide_Update");
    }

    public void Collide_Exit(PhysicsElement phyElem) {
        Gdx.app.log("GoldMinerObject", "Collide_Exit");
    }
}